package com.rzj.sys_lab_assistant.controller;

import com.rzj.sys_lab_assistant.entity.Department;
import com.rzj.sys_lab_assistant.entity.bo.DepartmentBO;
import com.rzj.sys_lab_assistant.entity.bo.DepartmentTypeBO;
import com.rzj.sys_lab_assistant.service.IDepartmentService;
import com.rzj.sys_lab_assistant.utils.ResultsUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * @program: sys_lab_assistant
 * @description:
 * @author: 作者
 * @create: 2021-06-29 10:48
 */
@Controller
@RequestMapping("department")
public class DepartmentController {

    @Autowired
    IDepartmentService departmentService;

    ResultsUtils resultsUtils;

    @RequestMapping("toAdd")
    public String toAdd(Model model) {
        model.addAttribute("type", departmentService.listDepartmentTypes());
        return "department/departmentAdd";
    }

    @RequestMapping("toEdit/{dId}")
    public String toUpdate(Model model, @PathVariable("dId") int dId) {
        model.addAttribute("type", departmentService.listDepartmentTypes());
        model.addAttribute("department", departmentService.getDepartmentById(dId));
        return "department/departmentUpdate";
    }

    @RequestMapping("departmentList")
    public String departmentList(Model model) {
        List<DepartmentBO> departmentList = departmentService.listDepartments();
        model.addAttribute("departmentList", departmentList);
        return "department/departmentList";
    }

    @RequestMapping("departmentAdd")
    @ResponseBody
    public Map<String, String> departmentAdd(@RequestParam("dId") String dId,
                                             @RequestParam("dName") String dName,
                                             @RequestParam("typeId") String typeId,
                                             @RequestParam("pid") String pid) {

        Department department = new Department(Integer.valueOf(dId), dName, Integer.valueOf(typeId), Integer.valueOf(pid));

        System.out.println("department==》"+department.toString());

        int flag = departmentService.addDepartment(department);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "添加成功！");
        } else {
            return resultsUtils.resultsMap("error", "添加失败，请联系管理员！");
        }

    }

    @RequestMapping("departmentEdit")
    @ResponseBody
    public Map<String, String> departmentEdit(@RequestParam("dId") String dId,
                                              @RequestParam("dName") String dName,
                                              @RequestParam("typeId") String typeId,
                                              @RequestParam("pid") String pid) {

        Department department = new Department(Integer.valueOf(dId), dName, Integer.valueOf(typeId), Integer.valueOf(pid));

        int flag = departmentService.editDepartment(department);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "修改成功！");
        } else {
            return resultsUtils.resultsMap("error", "修改失败，请联系管理员！");
        }

    }

    @RequestMapping("departmentDel")
    @ResponseBody
    public Map<String, String> departmentDel(@RequestParam("dId") int dId) {

        int flag = departmentService.delDepartment(dId);
        if (flag == 1) {
            return resultsUtils.resultsMap("success", "删除成功！");
        } else {
            return resultsUtils.resultsMap("error", "删除失败，请联系管理员！");
        }

    }

    @RequestMapping("departmentTree")
    @ResponseBody
    public List<DepartmentBO> departmentTree() {

        return departmentService.listDepartments();

    }




}
